<template>
  <a-modal
    v-model:visible="visible"
    :title="'分配角色'"
    draggable
    :width="width"
    @cancel="handleCancel"
  >
    <a-form :model="formData" ref="formRef" auto-label-width>
      <a-spin :loading="loading">
        <a-row>
          <a-form-item
            label="所属角色"
            field="roleIds"
            :rules="[{required:true,message:'角色不能为空'}]"
            :validate-trigger="['change']"
          >
            <a-select
              v-model="formData.roleIds"
              placeholder="请选择所属角色"
              allow-search
              multiple
              :filter-option="filterOption"
              allow-clear
            >
              <a-option
                v-for="item in roleData"
                :key="item.value"
                :value="item.value"
              >{{ item.label }}</a-option>
            </a-select>
          </a-form-item>
        </a-row>
      </a-spin>
    </a-form>
    <template #footer>
      <a-button
        style="margin-right: 8px"
        @click="handleCancel"
      >{{ $t('searchTable.columns.operations.close') }}</a-button>
      <a-button
        type="primary"
        @click="handleSubmit"
        :loading="loading"
      >{{ $t('searchTable.columns.operations.save') }}</a-button>
    </template>
  </a-modal>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { Modal, Message } from '@arco-design/web-vue';

import { getSysUser } from '@/api/sys/user';
import { authUser, UserRoleData } from '@/api/sys/role';
import { SelectData, selectRole } from '@/api/common';
import { filterOption } from '@/utils/common';

const { t } = useI18n();

const loading = ref(false);
const visible = ref(false);
const width = ref('calc(28%)');

// 表单
const formRef = ref<any>();
const formData = ref<UserRoleData>({});

// 角色列表
const roleData = ref<SelectData[] | undefined>([]);

// 打开弹窗
const onOpen = async (record: any) => {
  loading.value = true;
  await featchUserData(record.id);
  await featchRoleData();
  loading.value = false;
  visible.value = true;
};

// 确认
const handleSubmit = async () => {
  formRef.value.validate().then((result: any) => {
    // 校验不通过
    if (result) {
      return;
    }
    loading.value = true;
    authUser(formData.value)
      .then(() => {
        onOk();
        Message.success(t('operations.success'));
      })
      .finally(() => {
        loading.value = false;
      });
  });
};

// 取消
const handleCancel = () => {
  onOk();
};

// 确认
const onOk = () => {
  formRef.value.clearValidate();
  formData.value = {};
  visible.value = false;
};

// 获取用户数据
const featchUserData = async (id: any) => {
  getSysUser(id).then((res) => {
    formData.value.userId = res.data?.id;
    formData.value.roleIds = res.data?.roleIds;
  });
};

// 获取角色数据
const featchRoleData = async () => {
  selectRole({ status: 1 }).then((res) => {
    roleData.value = res.data;
  });
};

// 抛出函数
defineExpose({
  onOpen,
});
</script>
<style scoped lang="less">
</style>